var listType = require('list.js');
var todoItemType = require('todoItem.js');
var util = require('../utils/util.js');
var editItemType = require('editItem.js');
var topSwitchType = require('topSwitch.js');
var bottomSwitchType = require('bottomSwitch.js');

var colorCal = require('color.js');
var itemcolorCal = colorCal.itemColor;
var app = getApp();

(function(){
    var todoList = listType.createList();
    todoList.initTodoList = function(page, winWidth, winHeight, renderParam, collectList) {
        this.initList(page, winWidth, winHeight, renderParam);
        this.setAnimationParam('todoListAnimation');
        this.collectList = collectList;

        this.itemcolorCal = itemcolorCal;

        this.pullDownCreateText = '下拉创建事项';
        this.releaseCreateText = '释放创建事项';

        this.topItem = editItemType.createEditItem();
        this.topItem.init(this.page, this, 'todoTopItem', 0, true);
        this.topItem.setTop(-app.ITEM_HEIGHT);
        this.topItem.setColor('RGB(203, 0, 22)');
        this.topItem.setWidth(winWidth);
        this.topItem.setHeight(app.ITEM_HEIGHT);
        this.topItem.hide();
        this.bottomItem = editItemType.createEditItem();
        this.bottomItem.init(this.page, this, 'todoBottomItem', 0, false);
        this.bottomItem.setHeight(app.ITEM_HEIGHT);
        this.bottomItem.setWidth(winWidth);
        this.bottomItem.smoothRotateX(-90);
        this.bottomItem.hide();

        this.topSwitch = topSwitchType.createTopSwitch();
        this.topSwitch.init(this.page, this);
        this.topSwitch.hide();
        this.topSwitch.setTop(-app.ITEM_HEIGHT);
        this.topSwitch.setHeight(app.ITEM_HEIGHT);
        this.topSwitch.setText('切换到事项列表');

        this.bottomSwitch = bottomSwitchType.createBottomSwitch();
        this.bottomSwitch.init(this.page, this);
        this.bottomSwitch.setText('下拉清除完成事项');
        this.bottomSwitch.hide();
        this.renderPage();

        this.isEmpty = true;
        this.isDel = true;

        var s = this;
        this.setTouchcb(
            function(e, type, args) {

            },
            function(e, type, args) {
                s.dragmove(args);
            },
            function(e, type, args) {
                s.dragend(args);

            },
            function(e, type, args) {

            }
        );

    };

    todoList.load = function(collectItem, dataList) {
        this.setData(collectItem, dataList);
        this.updatePosition();
        this.moveY(this.lastPos);
    };

    todoList.reload = function(collectItem, dataList) {
        if (this.isDel) {
            this.setData(collectItem, dataList);
            return;
        }
        if (this.collectItem.getId() === collectItem.getId()) {
            this.resetPosition();
            this.renderPage();
            return;
        }
        this.setData(collectItem, dataList);
    };

    todoList.setData = function(collectItem, dataList) {
        this.collectItem = collectItem;
        this.lastPos = collectItem.data.lastPos;
        this.title = collectItem.getText();
        this.loadData(dataList);

        this.isDel = false;
        this.isEmpty = (dataList.length === 0);
    };

    todoList.save = function() {
        app.store.saveItemList(this.collectItem.getId(), this.dataList);
    };

    todoList.getTitle = function() {
        return this.title;
    };

    todoList.createItem = function(data) {
        var item = todoItemType.createItem();
        item.initItem(this.page, this, data, this.winWidth, this.renderParam);
        return item;
    };

    todoList.createNewData = function(order) {
        return app.store.createNewItem('', order, 1, 0);
    };

    todoList.openShow = function() {
        this.resetTopSwitch();
        this.updatePosition();
        var s = this.collectList;
        this.smoothMoveY(this.lastPos, function() {
            s.initPosition();
            s.positionForPullDown();
        });

        wx.setNavigationBarTitle({title:this.getTitle()});
    },

    todoList.pullUpShow = function() {
        var s = this;
        this.smoothMoveY(0, function() {
            s.resetTopSwitch();
            s.renderPage();
        });

        wx.setNavigationBarTitle({title:this.getTitle()});
    };

    todoList.positionForPullUp = function() {
        this.moveY(this.winHeight + app.CREATE_ITEM_THRESHOLD);
        this.topSwitch.setText(this.title);
        this.topSwitch.upArrow();
        this.topSwitch.hide();

        this.topItem.setText(this.pullDownCreateText);
        this.topItem.hide();
    };

    todoList.resetTopSwitch = function() {
        this.topSwitch.setText('切换到列表集合');
        this.topSwitch.hide();
        this.topSwitch.upArrow();
    }

    todoList.itemToRenderData = function(item, isMove) {
        var renderData = {};
        renderData.id = item.getId();
        renderData.display = item.display;
        renderData.text = item.getText();
        renderData.sliderColor = item.sliderColor;
        renderData.imgTop = item.imgTop;
        renderData.imgSize = item.imgSize;
        renderData.fadeTextDisplay = 'none';
        renderData.textVisibility = true;
        renderData.z_index = '1';
        renderData.height = item.height;
        renderData.opacity = item.opacity;
        if (item.done) {
            renderData.textDecoration = 'line-through';
            renderData.textColor = util.COLOR_GRAY;
        }
        else {
            renderData.textDecoration = 'none';
            renderData.textColor = util.COLOR_WHITE;
        }
        if (isMove) {
            renderData.animation = item.getMoveYData();
        }
        return renderData;
    };

    todoList.updateCount = function() {
        this.collectItem.setCount(this.items.length, this.count);
    };

    todoList.calDoneItemPosition = function() {
        var y = 0;
        for (var i = 0; i < this.items.length; i++) {
            if (!this.items[i].done) {
                y += this.items[i].height;
            }
        }
        return y;
    };

    todoList.calUnDoneItemPosition = function(target) {
        var y = 0;
        for (var i = 0; i < this.items.length; i++) {
            if (!this.items[i].done && this.items[i] !== target) {
                y += this.items[i].height;
            }
        }
        return y;
    };

    todoList.floatUp = function(at, target) {
        var height = target.height;
        var i = this.items.length,
            item;

        while (i--) {
            item = this.items[i];
            if (item.deleted || item === target) {
                continue;
            }
            else if (item.done) {
                if (item.getOrder() < at) {
                    item.plusOrder();
                    item.updatePosition(item.y + height);
                }
            }
            else {
                item.updateColor(this.count);
            }
        }
        this.renderPage();
    };

    todoList.doneItem = function(item) {
        var at = item.getOrder();
        this.dataList.splice(at, 1);
        this.dataList.splice(this.count - 1, 0, item.data);
        item.setColor(util.COLOR_BLACK);
        item.setLineStyle('line-through');
        item.setTextColor(util.COLOR_GRAY);
        var position = this.calDoneItemPosition();
        item.updatePosition(position, true);
        item.setOrder(this.count-1);
        this.count--;
        this.updateCount();
        this.collapseAt(at, item);

        this.save();
    };

    todoList.unDoneItem = function(item) {
        var at = item.getOrder();
        this.dataList.splice(at, 1);
        this.dataList.splice(this.count, 0, item.data);
        item.setTextColor(util.COLOR_WHITE);
        item.setOrder(this.count);
        item.setLineStyle('none');
        var position = this.calUnDoneItemPosition(item);
        item.updatePosition(position, true);
        this.count++;
        item.updateColor(this.count);
        this.updateCount();
        this.floatUp(at, item);

        this.save();
    };

    todoList.delItem = function(item) {
        this.count--;
        var at = item.getOrder();
        this.dataList.splice(at, 1);
        this.deleteItemById(item.id);
        this.updateCount();
        this.collapseAt(at, item);
        this.height -= item.height;
        this.updateBound(false);

        this.save();
    };

    todoList.getCollectId = function() {
        if (this.isDel) {
            return null;
        }
        return this.collectItem.getId();
    }

    todoList.doneAll = function() {
        var i = this.items.length;
        while (i--) {
            var item = this.items[i];
            item.doDone();
        }
        this.count = 0;
        this.renderPage();

        this.save();
    };

    todoList.del = function() {
        this.dataList = null;
        this.save();

        this.count = 0;
        this.isDel = this.isEmpty = true;
        this.items = [];
        this.collectItem = null;
        this.title = '';

        var renderData = this.page.getRenderData();
        renderData[this.renderParam] = [];
        this.topSwitch.hide();
        this.topSwitch.setText('');
        this.renderPage();
    };

    todoList.longPullUp = function() {
        if (!this._longPullUp) {
            this._longPullUp = true;
            var pos = Math.max(this.winHeight, this.height + app.ITEM_HEIGHT) + app.ITEM_HEIGHT;
            this.bottomSwitch.moveY(pos);
            this.bottomSwitch.show();
            this.bottomSwitch.setText('上拉清除完成事项');
            var doneItemCount = this.items.length - this.count;
            if (doneItemCount > 0) {
                this.bottomSwitch.unGray();
            }
            else {
                this.bottomSwitch.gray();
            }
        }

        // if (this.doneItemCount > 0) {
        //     var pos = (this.upBound - this.y) / (app.SWITCH_THRESHOLD) * (app.CREATE_ITEM_THRESHOLD + 15);
        //     this.bottomSwitch.moveArrowY(pos);
        // }
    };

    todoList.unLongPullUp = function() {
        if (this._longPullUp) {
            this._longPullUp = false;
            this.bottomSwitch.hide();
        }
    };

    todoList.pastLongPullUpThreshold = function() {
        if (!this._pastLongPullUpThreshold) {
            this._pastLongPullUpThreshold = true;
            var doneItemCount = this.items.length - this.count;
            if (doneItemCount > 0) {
                this.bottomSwitch.setText('释放清除完成事项');
            }       
        }
    };

    todoList.unPastLongPullUpThreshold = function() {
        if (this._pastLongPullUpThreshold) {
            this._pastLongPullUpThreshold = false;
            this.bottomSwitch.setText('上拉清除完成事项');
        }
    };

    todoList.longPullDown = function() {
        if (!this._longPullDown) {
            this._longPullDown = true;
            this.topSwitch.show();
            this.topItem.smoothHide();
        }
        this.collectList.moveY(this.y - app.SWITCH_THRESHOLD - this.collectList.height);
    };

    todoList.unLongPullDown = function() {
        if (!this._longPullDown) {
            return;
        }
        this._longPullDown = false;
        this.topSwitch.hide();
        this.topItem.smoothShow();
        this.collectList.moveY(-app.SWITCH_THRESHOLD - this.collectList.height);
    };

    todoList.onLongPullDown = function() {
        this.collectList.pullDownShow();
        var s = this;
        function callback() {
            s.positionForPullUp();
            s.renderPage();
        }
        this.smoothMoveY(this.winHeight + app.SWITCH_THRESHOLD, callback);
        this.page.currentList = this.collectList;
        app.setSetup(this.collectItem.getId(), util.SHOW_TYPE_COLLECT);
    };

    todoList.onPullUp = function() {
        var doneItemCount = this.items.length - this.count;
        if (doneItemCount <= 0) {
            return;
        }
        var i = this.items.length,
            item;

        var ids = [];
        
        while (i--) {
            item = this.items[i];
            if (item.done) {
                ids.push(item.getId());
                item.goAway();
                this.height -= item.height;
            }
        }
        
        i = ids.length;
        while (i--) {
            this.deleteItemById(ids[i]);
        }
        this.dataList.splice(this.count, doneItemCount);
        this.updateBound(true);

        this.save();
        this.updateCount();
    };

    todoList.dragmove = function(args) {
        if (this.y > 0) {
            this.pullDown();
        }
        else {
            this.unPullDown();
        }
        if (this.y <= app.CREATE_ITEM_THRESHOLD) {
            this.unPastPullDownThreshold();
        }
        else {
            this.pastPullDownThreshold();
        }
        if (this.y >= app.SWITCH_THRESHOLD) {
            this.longPullDown();
        }
        else {
            this.unLongPullDown();
        }
        if (this.y < this.upBound) {
            this.longPullUp();
        }
        else {
            this.unLongPullUp();
        }
        if (this.y < this.upBound - app.SWITCH_THRESHOLD) {
            this.pastLongPullUpThreshold();
        }
        else {
            this.unPastLongPullUpThreshold();
        }
        this.renderPage();
    };

    todoList.dragend = function(args) {
        this.resetMoveStates();
        if (this.y >= app.SWITCH_THRESHOLD) {
            this.onLongPullDown();
            args.isStop = true;
        }
        else if (this.y >= app.CREATE_ITEM_THRESHOLD) {
            this.onPullDown();
            args.isStop = true;
        }
        else if (this.y <= this.upBound - app.SWITCH_THRESHOLD) {
            this.onPullUp();
        }
    };

    todoList.ontap = function(e) {
        if (this.movingTouch) {
            this.movingTouch = false;
            return;
        }
        if (this.isSorting) {
            this.isSorting = false;
            return;
        }
        var item = this.getItemById(e.target.id);
        if (item && !item.done) {
            this.onEditStart(false, false, item);
            return;
        }
        this.onEditStart(true, false);
    };

    function createTodoList() {
        return Object.create(todoList);
    }

    module.exports = {
        createTodoList:createTodoList
    }
})();